package peng.yang.express.Activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Window;
import android.widget.FrameLayout;

import com.android.volley.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import FragmentUtils.FragmentUtils;
import peng.yang.express.Adapter.FragmentStatAdapterDemo;
import peng.yang.express.Bean.ListRes;
import peng.yang.express.Bean.PhoneMembers;
import peng.yang.express.Bean.SortModel;
import peng.yang.express.Fragment.Fragment01;
import peng.yang.express.Fragment.Fragment02;
import peng.yang.express.Fragment.Fragment03;
import peng.yang.express.R;
import peng.yang.express.Util.PhoneMembersDao;
import peng.yang.express.VolleyUtils.ImageSave;
import peng.yang.express.VolleyUtils.JsonArrayToList;
import peng.yang.express.VolleyUtils.RequestQueueUtils;

public class MainActivity extends AppCompatActivity {

    private LayoutInflater inflater;
    private TabLayout tabPager;
    private FragmentStatAdapterDemo adapter;
    private FrameLayout pagerExpress;
    private FragmentUtils fragmentUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestData();
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        fragmentUtil = FragmentUtils.getFragmentUtil(getSupportFragmentManager());
        fragmentUtil.addFragment(R.id.vpExpress,new Fragment01(),"Fragment01");
        findView();
        initData();
    }

    /**
     * 初始化控件
     */
    private void findView() {
        inflater = LayoutInflater.from(this);
        tabPager = (TabLayout) findViewById(R.id.tab_pager);
        pagerExpress = (FrameLayout) findViewById(R.id.vpExpress);
    }

    /**
     * 初始化数据
     */
    private void initData() {

        for(int i =0;i<3;i++) {
            tabPager.setTabTextColors(Color.GRAY,Color.RED);
            tabPager.setSelectedTabIndicatorHeight(0);
            tabPager.addTab(tabPager.newTab().setIcon(ListRes.getIcon().get(i)).setText(ListRes.getName().get(i)));

        }

        tabPager.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

            @Override
            public void onTabSelected(TabLayout.Tab tab) {

                switch (tab.getText().toString()){
                    case "快递查询":
                        fragmentUtil = FragmentUtils.getFragmentUtil(getSupportFragmentManager());
                        fragmentUtil.rePlaceFragment01(R.id.vpExpress,new Fragment01());
                        break;
                    case "查询历史":
                        fragmentUtil = FragmentUtils.getFragmentUtil(getSupportFragmentManager());
                        fragmentUtil.rePlaceFragment01(R.id.vpExpress,new Fragment02());
                        break;

                    case "快递电话":
                        fragmentUtil = FragmentUtils.getFragmentUtil(getSupportFragmentManager());
                        fragmentUtil.rePlaceFragment01(R.id.vpExpress,new Fragment03());
                        break;
                }

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });



    }

    private void requestData() {
        PhoneMembersDao dao = new PhoneMembersDao(getApplicationContext());
        List<PhoneMembers> list = dao.getAllMembers();
        if(list.size()==0){
            String url = "http://route.showapi.com/64-20?showapi_appid=20075&showapi_sign=a40bc8ce822d42b585b28d5b39cb9953";
            RequestQueueUtils.exectueJsonRequest(this, url, new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject jsonObject) {
                    try {

                        JSONArray jsonArray = jsonObject.getJSONObject("showapi_res_body").getJSONArray("expressList");
                        JsonArrayToList toList = new JsonArrayToList(jsonArray);
                        List<SortModel> SourceDateList = toList.setJRToList();
                        ImageSave save = new ImageSave(SourceDateList,MainActivity.this);
                        save.imageSave();
                        Log.e("TAG,_____+++++ ", "" + SourceDateList.size());

                    } catch (JSONException e) {
                        e.printStackTrace();
                        Log.e("====printStackTrace",e.getMessage());
                    }
                }
            });

        }

    }

}
